<template>
	<view class="rate-comtainer">
		<view class="flex mb20" v-for="(item, index) in commentList" :key="index">
			<text class="f24 c343">{{ item.areaValue }}</text>
			<view class="ml20 mr20">
				<u-rate v-model="item.rate" gutter="1" activeIcon="star-fill" inactiveIcon="star-fill" 
					inactiveColor="#E6E6EA" margin="30" activeColor="#FFD955" :readonly="readonly" @change.stop="handleRate(item, index, $event)"></u-rate>
			</view>
			<text class="f24 c999">{{item.rateText}}</text>
		</view>
	</view>
</template>

<script>
	import {
		getGoodsComment
	} from '@/common/api_order.js'
	export default {
		props: {
			// 是否只读
			readonly: {
				type: Boolean,
				default: false
			},
			// 请求参数
			rateParams: {
				type: Object,
				required: true
			},
			// 真实打分
			stepScores: {
				type: Array,
				default: () => []
			}
		},
		computed: {
			commentList() {
				let tempList = []
				if (this.stepScores.length) {
					// 评价详情
					tempList = this.rateTypeList.map((item, index) => ({
						rate: this.stepScores[index],
						areaValue: item.areaValue,
						rateText:  this.rateMapText[this.stepScores[index]]
					}))
				} else {
					// 评价
					tempList = this.rateTypeList.map((item, index) => ({
						rate: item.rate,
						areaValue: item.areaValue,
						rateText:  item.rateText
					}))
				}
				
				return tempList
			}
		},
		data() {
			return {
				rateTypeList: [], // 评价标签
				rateMapText: ['', '非常差', '差', '一般', '好', '非常好'],
			}
		},
		watch: {
			rateParams: {
				handler(newVal, oldVal){
					if (newVal && newVal.id) {
						let type = ''
						// orderType 1家政、2开锁、3团购
						if (newVal.orderType === 1) {
							type = newVal.orderPro?.proType || newVal.goodType
						} else if (newVal.orderType === 3) {
							type = newVal.goodsInfo?.goodType || newVal.goodType
						} else { 
							type = newVal.goodType
						}
						console.log(newVal, 'newVal')
						// let oType = newVal.orderType ? newVal.orderType : this.orderType ? this.orderType : ''
						this.getRateLabels(newVal.orderType, type)
					}
				},
				immediate: true,
				deep: true
			}
		},
		methods: {
			// 打分
			handleRate(item, index, e) {
				this.rateTypeList[index].rate = e
				this.rateTypeList[index].rateText = this.rateMapText[e]
				// 给父组件传打分数组
				this.$emit('getRate', this.commentList)
			},
			// 商品评价类型
			async getRateLabels(orderType, goodType) {
				uni.showLoading({
					mask: true
				})
				const res = await getGoodsComment(orderType, goodType)
				if (res.code == 200) {
					if (res.data.length) {
						res.data.forEach(item => {
							item.rate = 5
							item.rateText = '非常好'
						})
						console.log(res.data, '123456')
						this.rateTypeList = res.data
					}
				}
				uni.hideLoading()
			},
		}
	}
</script>

<style>
</style>